/**
 * 与资料相关的数据库操作
 */
package com.xiaomuwu.android.logic.dao

import android.util.Log
import com.xiaomuwu.android.logic.model.*
import com.xiaomuwu.android.ui.Displayer
import java.sql.Connection
import java.sql.ResultSet
import kotlin.concurrent.thread

object ResData {

    /**
     * 获取资料列表
     */
    fun getRes(forRes:ForRes,connection:Connection?):List<Res>?{
        return run {
            val resName=if(forRes.resName==null){
                null
            }else{
                "'${forRes.resName}'"
            }
            val resTags=if(forRes.resTags==null) {
                null
            }else{
                "'${forRes.resTags}'"
            }
            val sql="exec getRes $resName,$resTags,'${forRes.sortBy}'"
            var result:ResultSet?=null
            try {
    //                Thread{
                result=connection?.prepareStatement(sql)?.executeQuery()
                Log.d("ResData","getRes操作成功")
    //                }.start()
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("ResData","getRes操作失败")
            }
            if(result!=null){
                val dataResult=ArrayList<Res>()
                while(result.next()){
                    dataResult.add(Res(result.getLong("Res_ID"),
                        result.getString("Res_Name"),
                        result.getString("Tags")))
                }
                dataResult
            }else{
                result
            }
        }
    }

    /**
     * 获取相关资料（收藏、上传）列表
     */
    fun getReleRes(forReleRes: ForReleRes, connection: Connection?):List<Res>?{
        return run{
            lateinit var sql:String
            val resName=if(forReleRes.resName==null){
                null
            }else{
                "'${forReleRes.resName}'"
            }
            when(Displayer.pageStatus){
                "Favorites"->{
                    sql ="exec getFavorites '${forReleRes.userID}',$resName"
                }
                "Upload"->{
                    sql ="exec getUpload '${forReleRes.userID}',$resName"
                }
            }
            var result:ResultSet?=null
            try {
                //                Thread{
                result=connection?.prepareStatement(sql)?.executeQuery()
                Log.d("ResData","getReleRes操作成功")
                //                }.start()
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("ResData","getReleRes操作失败")
            }
            if(result!=null){
                val dataResult=ArrayList<Res>()
                while(result.next()){
                    dataResult.add(Res(result.getLong("Res_ID"),
                        result.getString("Res_Name"),
                        result.getString("Tags")))
                }
                dataResult
            }else{
                result
            }
        }
    }

    /**
     * 获取资料详情
     */
    fun getResDetail(forResDetail: ForResDetail,connection: Connection?):ResDetail?{
        return run{
            val userID=if(forResDetail.userID==null){
                null
            }else{
                "'${forResDetail.userID}'"
            }
            val sql="exec getResDetail $userID,${forResDetail.resID}"
            var result:ResultSet?=null
            try{
//                Thread{
                    result=connection?.prepareStatement(sql)?.executeQuery()
                    Log.d("ResData","getResDetail操作成功")
//                }.start()
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("ResData","getResDetail操作失败")
            }
            if (result!=null){
                result.next()
                ResDetail(result.getString("Uploader"),
                    result.getString("Res_Link"),
                    result.getString("About"),
                    result.getInt("Praise_Count"),
                    result.getInt("Down_Count"),
                    result.getInt("Collect_Count"),
                    result.getInt("IsPraised"),
                    result.getInt("IsCollected"))
            }else{
                result
            }
        }
    }

    /**
     * 点赞/收藏
     */
    fun likeRes(forLike: ForLike,connection: Connection?){
        val sql="exec likeRes '${forLike.userID}',${forLike.resID},'${forLike.likeForm}'," +
                "'${forLike.action}'"
        try {
            thread{
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("ResData","likeRes操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("ResData","likeRes操作失败")
        }
    }

    /**
     * 下载
     */
    fun downloadRes(forDownLoad: ForDownLoad,connection: Connection?){
        val sql="exec downloadRes '${forDownLoad.userID}',${forDownLoad.resID}"
        try {
            thread{
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("ResData","downloadRes操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("ResData","downloadRes操作失败")
        }
    }

    /**
     * 上传资料
     */
    fun uploadRes(forUpload: ForUpload, connection: Connection?){
        val tags=if(forUpload.tags==null){
            null
        }else{
            "'${forUpload.tags}'"
        }
        val about=if(forUpload.about==null){
            null
        }else{
            "'${forUpload.about}'"
        }
        val sql="exec uploadRes '${forUpload.resName}',$tags,'${forUpload.resLink}'," +
                "'${forUpload.uploader}',$about"
        try {
            thread {
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("ResData","uploadRes操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("ResData","uploadRes操作失败")
        }
    }

    /**
     * 修改资料
     */
    fun modifyRes(forModify: ForModify,connection: Connection?){
        val tags=if(forModify.tags==null){
            null
        }else{
            "'${forModify.tags}'"
        }
        val about=if(forModify.about==null){
            null
        }else{
            "'${forModify.about}'"
        }
        val sql="exec modifyRes ${forModify.resID},'${forModify.resName}',$tags," +
                "'${forModify.resLink}','${forModify.uploader}',$about"
        try {
            thread {
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("ResData","modifyRes操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("ResData","modifyRes操作失败")
        }
    }

    /**
     * 取消分享
     */
    fun deleteRes(resID:Long,connection: Connection?){
        val sql="exec deleteRes $resID"
        try {
            thread {
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("ResData","deleteRes操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("ResData","deleteRes操作失败")
        }
    }

}